# -*- coding: utf-8 -*-
import sys
import yaml
import ConnMySql
import ConfigParser
from JKlog import *

reload(sys)
sys.setdefaultencoding('utf-8')


class DbClear:
    def __init__(self, strConfig, bank_card):
        self.bank_card = bank_card
        self.fr = open('Config.yml', 'r')
        self.fconfig = yaml.load(self.fr)
        self.realConfig = self.fconfig[strConfig]

    def dataClear(self):
        flag = False  # acc 库初始化
        account = self.realConfig["account"]
        host = account["host"]
        user = account["user"]
        pwd = account["password"]
        name = account["name"]
        port = account["port"]
        charset = account["charset"]
        accDb = ConnMySql.ConnMySql(host, port, name, user, pwd, charset)

        # pay 库初始化
        pay = self.realConfig["pay"]
        host = pay["host"]
        user = pay["user"]
        pwd = pay["password"]
        name = pay["name"]
        port = pay["port"]
        charset = pay["charset"]
        accPDb = ConnMySql.ConnMySql(host, port, name, user, pwd, charset)

        # 定义用到的变量
        ACCOUNT_ID = False
        self.bank_card_ID = False

        # 记录日志，以 UID 命名
        logger = JKLogger('BankCardClean' + "_" + self.bank_card)

        try:
            # ACCOUNT 库
            # 获取 ACCOUNT_ID 且是唯一的
            SQL30 = 'SELECT * FROM tbl_bank_card WHERE self.bank_card = "%s"' % self.bank_card
            result01 = accDb.fetch_all(SQL30)
            if result01:
                self.bank_card_ID = result01[0]['ID']
                ACCOUNT_ID = result01[0]['ACCOUNT_ID']
                logger.log(u'获得的 ACCOUNT_ID 参数值为: %s ' % ACCOUNT_ID)
                logger.log(u'获得的 self.bank_card_ID 参数值为: %s ' % self.bank_card_ID)
            else:
                logger.log(u'self.bank_card在 tbl_bank_card 表中不存在，请确认卡号 ')

            logger.log(u'第二步：查询所有相关数据库中的表数据----------------------------------------------------------')
            if self.bank_card_ID:
                result02 = accDb.fetch_all(SQL30)
                logger.log(u'获得的tbl_bank_card表数据为: %s ' % result02)
                SQL31 = 'SELECT * FROM tbl_wallet_payment_method WHERE account_id = "%s"' % ACCOUNT_ID + 'AND ID = "%s"' % self.bank_card_ID
                result03 = accDb.fetch_all(SQL31)
                j = 0
                for i in result03:
                    logger.log(u'获得的tbl_wallet_payment_method表数据为: %s ' % result03[j])
                    j += 1
                SQL32 = 'SELECT * FROM pay_unionpay_token_open_card WHERE self.bank_card = "%s"' % self.bank_card
                result04 = accPDb.fetch_all(SQL32)
                logger.log(u'获得的 pay_unionpay_token_open_card 表数据为: %s ' % result04)
            else:
                logger.log(u'无法获得数据库表数据，请确认参数')

            logger.log(u'第三步：删除所有相关数据库中的表数据----------------------------------------------------------')
            if self.bank_card_ID:
                DSQL30 = 'DELETE FROM tbl_bank_card WHERE self.bank_card = "%s"' % self.bank_card
                Dresult02 = accDb.update(DSQL30)
                logger.log(u'删除 tbl_bank_card 表数据为: %s ' % Dresult02)
                DSQL31 = 'DELETE FROM tbl_wallet_payment_method WHERE account_id = "%s"' % ACCOUNT_ID + 'AND ID = "%s"' % self.bank_card_ID
                Dresult03 = accDb.update(DSQL31)
                logger.log(u'删除 tbl_wallet_payment_method 表数据为: %s ' % Dresult03)
                DSQL32 = 'DELETE FROM pay_unionpay_token_open_card WHERE self.bank_card = "%s"' % self.bank_card
                Dresult04 = accPDb.update(DSQL32)
                logger.log(u'删除 pay_unionpay_token_open_card 表数据为: %s ' % Dresult04)
            else:
                logger.log(u'无法删除数据库表数据，请确认参数')

            logger.log(u'第四步，数据清空完毕，可重复执行，查找数据均为空-----------------------------------------------')
            # mem 库关闭
            accDb.close()
            accPDb.close()
            flag = True
        except Exception, data:
            logger.logger(data.message)
            flag = False
        logger.delLogger()
        return flag






